package org.grow.preview;

import org.grow.preview.domain.Bill;
import org.grow.preview.domain.Cargo;
import org.grow.preview.domain.domainUtils.RandomUtil;
import org.grow.preview.infrastructure.JpaBill;
import org.grow.preview.infrastructure.JpaCargo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.persistence.EntityManager;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SunnyDay {

    @Resource
    private JpaBill jpaBill;
    @Resource
    private JpaCargo jpaCargo;

    @Resource
    private RandomUtil randomUtil;

    private Bill randomBill(){
        Bill bill = new Bill();
        bill.setBillCode(randomUtil.randomBusinessCode(4));
        return bill;
    }

    private Cargo randomCargo(){
        Cargo cargo = new Cargo();
        cargo.setCargoCode(randomUtil.randomBusinessCode(8));
        cargo.setCargoName("Rubbish"+randomUtil.nextInt(1000));
        return cargo;
    }
    @Test
    public void test01(){
        Cargo cargo1 = this.randomCargo();
        Cargo cargo2 =this.randomCargo();
        Bill bill = this.randomBill();
        bill.getCargos().add(cargo1);
        bill.getCargos().add(cargo2);

        System.out.println(bill);

        jpaCargo.save(cargo1);
        jpaCargo.save(cargo2);
        jpaBill.save(bill);
        System.out.println(bill);
    }
    //need fetchType = eager
    String billCode ="S4968";
    String cargoCode="Sa47014fd";
    @Test
    public void test02(){
       // System.out.println(jpaBill.findByBillCode(billCode));
        System.out.println(jpaBill.queryByBillCode(billCode));

    }
    @Test
    public void test03(){
        System.out.println(jpaBill.searchByBillCode(billCode));
    }
    @Test
    public void test04(){
        System.out.println(jpaBill.searchByCargoCode(cargoCode));
    }
    @Value("#{randomUtil.a}")
    String a;
    @Resource
    private EntityManager entityManager;
    @Test
    public void test05(){
        System.out.println(a);
        System.out.println(entityManager.createNativeQuery(a,Bill.class).getResultList());

    }

}

